/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Actor.weapons;

import Actor.Actor;
import actor.weapons.Weapon;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import projectshanghai.World;

/**
 *
 * @author Eric-Standard
 */
public class LaserGun extends Weapon {

    protected Rectangle2D r = new Rectangle2D.Double();
    protected int width = 5;
    protected int height = 10;
    public static final int DEFAULT_DAMAGE = 25;
    protected double heat = 0;
    //Timer from last firing
    protected double heatCooldownTime = 0;
    protected double heatCost = 5;
    protected double maxHeat = 100;
    protected double heatCooldownRate = 50;
    protected double heatCooldownTimeout = 200;
    protected double overheatTimeout = 2000;

    public LaserGun(Actor a) {
        this(a, DEFAULT_DAMAGE);
    }

    public LaserGun(Actor a, int damage) {
        super(a);
        this.ammo = -1;
        this.totalAmmo = -1;
        this.clipSize = -1;
        this.damage = damage;
        this.cooldownTimeout = 200;
        this.reloadTimeout = -1;
    }

    @Override
    public void act(long dt) {
        super.act(dt);
        if (!this.firing && heat > 0) {
            if (heatCooldownTime <= 0) {
                heat -= heatCooldownRate * dt / 1000f;
            } else {
                //Decrease the cooldownTime
                heatCooldownTime -= dt;
                if (heatCooldownTime < 0) {
                    heatCooldownTime = 0;
                }
            }
            if (heat < 0) {
                heat = 0;
            }
        }
    }

    @Override
    public boolean fire() {
        if (heat >= maxHeat || cooldownTime > 0) {
            return false;
        }
        firing = true;
        Laser laser = new Laser(owner, damage);
        owner.getWorld().add(laser);
        heat += heatCost;
        cooldownTime = cooldownTimeout;
        heatCooldownTime = heatCooldownTimeout;
        if (heat >= maxHeat) {
            heatCooldownTime = overheatTimeout;
        }
        return true;
    }

    @Override
    public void draw(Graphics g) {
        g.setColor(Color.GRAY);
        g.drawRect((int) pos.x, (int) pos.y, width, height);
    }

    @Override
    public Rectangle2D getBoundingRectangle() {
        r.setRect(pos.x, pos.y, width, height);
        return r;
    }

    @Override
    public void drawHUD(Graphics g) {
        Dimension size = world.getSize();
        int rectX = size.width - World.HUD_WIDTH + 1;
        int rectY = size.height - World.HUD_HEIGHT + 1;
        g.setColor(Color.WHITE);
        g.drawRect(rectX, rectY, 51, 21);
        if (g instanceof Graphics2D) {
            Graphics2D gr = (Graphics2D) g;
            gr.setPaint(new GradientPaint(rectX, 0, Color.GREEN, rectX + 50, 0, Color.RED));
            gr.fillRect(rectX + 1, rectY + 1, (int) (1d * heat / maxHeat * 50), 20);
        } else {
            g.setColor(Color.RED);
            g.fillRect(rectX + 1, rectY + 1, (int) (1d * heat / maxHeat * 50), 20);
        }
    }
}
